//
//  ViewController.m
//  DM
//
//  Created by XingShuai on 2020/9/23.
//  Copyright © 2020 dashuai. All rights reserved.
//

#import "ViewController.h"

#import "DSHBarrageView.h"
#import "DMCell.h"

@interface ViewController () <DSHBarrageViewDelegate>

@property (strong ,nonatomic) DSHBarrageView *dmView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _dmView = [[DSHBarrageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _dmView.topSpacing = 88; // 顶部距离
    _dmView.cellClass = [DMCell class];  // 弹道样式
    _dmView.channelHeight = 30.f;        // 弹道高度
    _dmView.lineSpacing = 20.f;          // 间距
    _dmView.channelCount = 3;            // 弹道个数
    _dmView.delegate = self;             // 点弹幕代理
    [self.view addSubview:_dmView];
    
    // 测试数据
    for (int i = 0; i < 50; i ++) {
        DSHBarrageMessage *message = [[DSHBarrageMessage alloc] init];
        message.body = [self randomMessage];
        message.speed = 0;
        [self.dmView receivedMessage:message];
    }
}

#pragma mark - 点击空白追加弹幕
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    DSHBarrageMessage *message = [[DSHBarrageMessage alloc] init];
    message.body = [self clickRandomMessage];
    message.speed = 0;
    [self.dmView receivedMessage:message];
}

// 代理
- (void)barrageView:(DSHBarrageView *)barrageView clickedBarrageCell:(DSHBarrageViewCell *)barrageViewCell; {
    NSDictionary *body = barrageViewCell.message.body;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"第%@条弹幕" ,body[@"id"]] message:[NSString stringWithFormat:@"%@:%@" ,body[@"nickname"] ,body[@"content"]] preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"好的" style:0 handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}


#pragma mark - 模拟数据
- (NSDictionary *)randomMessage {
    static NSInteger index = 0;
    index ++;
    return @{@"color":[self randomColor],
             @"content":[self randomContent],
             @"nickname":@"大帅",
             @"head":[self randomHead],
             @"id":@(index),
             @"type":@(arc4random() % 3)};
}

#pragma mark - 模拟数据
- (NSDictionary *)clickRandomMessage {
    static NSInteger index = 0;
    index ++;
    return @{@"color":[self randomColor],
             @"content":@"大帅你正tnn的帅呀的不行不行的呀",
             @"nickname":@"大帅",
             @"head":[self randomHead],
             @"id":@(index),
             @"type":@(arc4random() % 3)};
}

- (UIColor *)randomColor {
    return [UIColor blackColor];
}
- (NSString *)randomContent {
    static NSArray *array = nil;
    if (!array) {
        array = @[@"红红火火恍恍惚惚",
                  @"你今天真好看",
                  @"那个xx你真逗，呵呵",
                  @"这波操作可以",
                  @"溜了溜了，拜拜",
                  @"666666666666666666666",
                  @"半醉半醒日复日，花落或开年复年",
                  @"小姐姐我可以点个歌吗",];
    }
    return array[arc4random() % array.count];
}


- (NSString *)randomHead {
    static NSArray *array = nil;
    if (!array) {
        array = @[@"https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1899397357,205586921&fm=27&gp=0.jpg",
                  @"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2951924714,3607611016&fm=27&gp=0.jpg",
                  @"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3069244644,2570574027&fm=27&gp=0.jpg",
                  @"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2802691956,955693789&fm=27&gp=0.jpg",
                  @"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3336226061,4016676102&fm=27&gp=0.jpg",];
    }
    return array[arc4random() % array.count];
}

- (void)dealloc {
    [_dmView destroy];
}

@end
